<?php

namespace App\Console\Commands\User;

use App\Enums\Common\NsqEnum;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopLocationModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageUserModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignModel;
use App\Models\OpenShop\ShopCheckAccept\OpenShopCheckAcceptModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\WorkOrder\WorkOrderApplyManagerModel;
use App\Models\WorkOrder\WorkOrderUserRoleModel;
use Illuminate\Console\Command;
use Kuafu\Nsq\NsqSubscriber;
use NSQClient\Message\Message;

class SubRmsUserEditCommand extends Command
{
    protected $signature = 'nsq:sub-rms-user-edit';

    protected $description = '订阅rms用户编辑事件';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        (new NsqSubscriber(config('nsq.default.address')))
            ->sub(NsqEnum::TOPIC_RMS_USER_BASE_INFO_EDIT, NsqEnum::CHANNEL_TIGER_SUB_USER, function (Message $message) {
                $data = $message->data();

                $userId = $data['changes']['id'] ?? 0;
                if (!$userId) {
                    $message->done();
                }

                // 用户名变更
                if (isset($data['changes']['username'])) {
                    $this->do($userId, $data['changes']['username']);
                }

                $message->done();
            });
    }

    private function do(int $userId, string $username)
    {
        OpenShopSignOrderModel::query()
                              ->where('investmentManagerId', $userId)
                              ->update([
                                  "investmentManagerName" => $username
                              ]);

        OpenShopLocationModel::query()
                             ->where("investmentManagerId", $userId)
                             ->update([
                                 "investmentManagerName" => $username
                             ]);

        OpenShopSiteLocationModel::query()
                                 ->where("developConsultantId", $userId)
                                 ->update([
                                     "developConsultantName" => $username
                                 ]);

        OpenShopSpaceDesignModel::query()
                                ->where("designUid", $userId)
                                ->update([
                                    "designName" => $username
                                ]);

        OpenShopCheckAcceptModel::query()
                                ->where("managerId", $userId)
                                ->update([
                                    "managerName" => $username
                                ]);

        OpenShopOrderManagerModel::query()->where('managerUid', $userId)->update(['managerName' => $username]);

        OpenShopOrganizationManageUserModel::query()->where('userId', $userId)->update(['name' => $username]);

        WorkOrderApplyManagerModel::query()->where('managerId', $userId)->update(['managerName' => $username]);

        WorkOrderUserRoleModel::query()->where('userId', $userId)->update(['name' => $username]);

        CruiseCheckDetailModel::query()->where('dealUid', $userId)->update(['dealName' => $username]);
    }
}
